//
//  YCAlertView.m
//  HeartWorldClient
//
//  Created by Mac on 2017/7/14.
//  Copyright © 2017年 谭明权. All rights reserved.
//

#import "YCAlertView.h"

@interface YCAlertView()

//遮罩
@property (nonatomic, strong) UIView *maskView;
//容器
@property (nonatomic, strong) UIView *containerView;

@end

@implementation YCAlertView

//创建单例
+ (instancetype)sharedAlertView {
    static YCAlertView *_instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[YCAlertView alloc] init];
    });
    return _instance;
}

- (void)showAlertView{
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    self.maskView = [[UIView alloc] initWithFrame:window.bounds];
    self.maskView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.50];
    [window addSubview:self.maskView];
    UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor = [UIColor whiteColor];
    
    contentView.layer.cornerRadius = 5;
    contentView.layer.masksToBounds = YES;
    
    [window addSubview:contentView];
    self.containerView = contentView;
    
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.mas_equalTo(SCREEN_WIDTH-80);
        
        make.height.mas_equalTo(100);
        
        make.centerX.equalTo(window.mas_centerX);
        
        make.top.equalTo(window.mas_top).offset(160);
        
    }];
    
    UILabel *label1 = [[UILabel alloc]init];
    label1.text = @"收不到短信验证码?";
    label1.textColor = [UIColor lightGrayColor];
    label1.font = [UIFont systemFontOfSize:15];
    label1.textAlignment = NSTextAlignmentCenter;
    [contentView addSubview:label1];
    

    
    UILabel *label2 = [[UILabel alloc]init];
    label2.text = @"请点击语音验证码获取验证码";
    label2.textColor = [UIColor lightGrayColor];
    label2.font = [UIFont systemFontOfSize:15];
    label2.textAlignment = NSTextAlignmentCenter;
    [contentView addSubview:label2];
    
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(contentView.mas_centerX);
        make.top.equalTo(contentView).offset(25);
        
    }];
    
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(contentView.mas_centerX);
        make.bottom.equalTo(contentView).offset(-25);
    }];
    weakify(self);
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        strongify(self)
        
        [self dimiss];
        
    });
    
    

}

- (void)dimiss {
    __weak __typeof(&*self)mySelf = self;
    
    [UIView animateWithDuration:0.25 animations:^{
       
        mySelf.containerView.transform = CGAffineTransformMakeScale(0.6, 0.6);
        mySelf.alpha = 0;
        mySelf.maskView.alpha = 0;
        mySelf.containerView.alpha = 0;
    }
                     completion:^(BOOL finished) {
                         [mySelf removeFromSuperview];
                         [mySelf.maskView removeFromSuperview];
                         [mySelf.containerView removeFromSuperview];
                         
                     }];
}

@end
